package com.sf.web.controller;

import com.alibaba.druid.pool.DruidDataSource;
import com.sf.domain.Department;
import com.sf.qo.EmployeeQueryObject;
import com.sf.qo.QueryObject;
import com.sf.service.IDepartment;
import com.sf.utils.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/department")
public class DepartmentController {
    @Autowired
    private IDepartment department;

    @RequestMapping("/list")

    public String list(EmployeeQueryObject qo, Model model) {
        PageResult<Department> pageResult = department.query(qo);
        model.addAttribute("pageResult", pageResult);
        return "department/list";
    }
    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
            department.deleteByPrimaryKey(id);
        }
        return "redirect:/department/list"; // 让浏览器再发送请求到 /department/list
    }

    // 处理跳转到一个页面请求, 响应 HTML 内容，页面有一个表单

    // 在 webapp 搞一个 jsp, 添加的时候访问那个  jsp（不遵循 MVC 思想）
    // 在 webapp 搞一个 HTML , 添加的时候访问那个 HTML（修改的时候回显数据这个做不到）
    // 编写一个 Servlet 转发到一个 jsp 就可以了
    // 处理方法

    // /department/input
    @RequestMapping("/input")
    public String input(Long id, Model model) {
        // id 有值  去修改
        // id null 去新增
        if (id != null) { // 去修改
            Department department1 = department.get(id);

            model.addAttribute("department", department1);


        }
         return "department/input";
    }

    // /department/saveOrUpdate 参数在请求体重
    // 处理保存部门的请求
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Department department1) {
        System.out.println(department1.getId());

            department.updateByPrimaryKey(department1);



//        if (department1.getId() == null) {
//            department.insert(department1);
//        } else {
//            department.updateByPrimaryKey(department1.getId());
//        }


        return "redirect:/department/list"; // 让浏览器再发送请求到 /department/list
    }

}
